package com.mfin.bom.factory;

import com.mfin.bom.adapter.AddressAdapter;
import com.mfin.bom.adapter.CustomerAdapter;
import com.mfin.bom.adapter.DocChargeAdapter;
import com.mfin.bom.adapter.FinanceLoanAdapter;
import com.mfin.bom.adapter.PartnerAdapter;
import com.mfin.bom.factory.interfaces.ICustomerInformationFactory;
import com.mfin.bom.interfaces.IAddress;
import com.mfin.bom.interfaces.ICustomer;
import com.mfin.bom.interfaces.IFinanceLoan;
import com.mfin.bom.interfaces.IPartner;
import com.mfin.bom.objects.Address;
import com.mfin.bom.objects.Customer;
import com.mfin.bom.objects.FinanceLoan;
import com.mfin.bom.objects.Partner;
import com.mfin.vo.ObjectKey;
import com.mfin.vo.interfaces.IIdentifiableObject;

public class CustomerInformationFactory extends BaseFactory implements
		ICustomerInformationFactory {

	public ICustomer getCustomerInformation(
			IIdentifiableObject identifiableObject) {
		ICustomer ca = null;
		ObjectKey[] keys = identifiableObject.getKeys();

		try {
			Customer c = (Customer) getPersistenceGovernor().get(
					Customer.class, keys[1].getId());

			if (c != null) {
				ca = new CustomerAdapter(c, keys);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return ca;
	}

	@Override
	public IPartner getPartnerInformation(IIdentifiableObject identifiableObject) {
		IPartner ca = null;
		ObjectKey[] keys = identifiableObject.getKeys();

		try {
			Partner c = (Partner) getPersistenceGovernor().get(Partner.class,
					keys[1].getId());

			if (c != null) {
				ca = new PartnerAdapter(c, keys);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return ca;
	}

	@Override
	public IFinanceLoan createLoan(IIdentifiableObject identifiableObject) {
		IFinanceLoan la = null;
		ObjectKey[] keys = identifiableObject.getKeys();

		try {
			FinanceLoan l = new FinanceLoan();

			if (l != null) {
				getPersistenceGovernor().insert(l);
				la = new FinanceLoanAdapter(l, keys);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return la;
	}

	@Override
	public IAddress createAddress() {
		IAddress ad = null;
		try {
			Address a = new Address();

			if (a != null) {
				getPersistenceGovernor().insert(a);
				ad = new AddressAdapter(a);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return ad;
	}
}
